package org.zeroes.Components;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.zeroes.Events.Callback;
import org.zeroes.Events.ZSPair;

import android.util.Log;

import com.zeroes.cons.ZSGameManager;


public class ZSComponent {
	
	protected List<ZSPair<?,?>> logic=new ArrayList<ZSPair<?,?>>();
	protected List<String> names;
	protected List<Callback> cbs;
	public String name;
	public ZSComponent(String n, ZSLUParameters p, ZSLogicUnit c){
		
		this.logic.add(new ZSPair<String,ZSLogicUnit>(n+".constructor.*",c));
		names=Collections.synchronizedList(new ArrayList<String>());	
		cbs=Collections.synchronizedList(new ArrayList<Callback>());	
		this.name=n;
		ZSGameManager.AskMe().forCurrentGame().registerComponent(this);
		c.is(this,p);
	}
	
	public void AddProperty(String def, ZSSingle<?> val){
		
		this.logic.add(new ZSPair<String,ZSSingle<?>>(def,val));
	}
	
	public ZSSingle<?> getProperty(String name){
		
		for(ZSPair<?,?> entry : logic){
			String defin = (String)entry.getFirst();
			String[] items = defin.split("\\.");
			if(items[1].equals("property")&&items[2].equals(name)){
				
				return (ZSSingle<?>)entry.getSecond();
			}
			
		}
		return null;
	}
	
	public void setProperty(String _name,ZSSingle<?> newval){
		
		String prodef=null;
		int idx=0,fidx=0;
		for(ZSPair<?,?> entry : logic){
			String defin = (String)entry.getFirst();
			String[] items = defin.split("\\.");
			if(items[1].equals("property")&&items[2].equals(_name)){
				prodef=defin;

			fidx=idx;
				
			}
			idx++;
			
		}
		if(fidx>0&&prodef!=null)
		logic.set(fidx, new ZSPair<String,ZSSingle<?>>(prodef,newval));
		
	}
	
// STRING
	
	public void AddStringProperty(String def, String val){
		
		this.logic.add(new ZSPair<String,ZSSingle<?>>(def,new ZSSingle<String>(val)));
	
	}
	
	public String getStringProperty(String name){
		
		for(ZSPair<?,?> entry : logic){
			String defin = (String)entry.getFirst();
			String[] items = defin.split("\\.");
			if(items[1].equals("property")&&items[2].equals(name)){
				ZSSingle<?> v = (ZSSingle<?>)entry.getSecond();
				return (String)v.getFirst();
			}
			
		}
		return null;
	}
	
	public void setStringProperty(String _name,String newstring){
		
		String prodef=null;
		int idx=0,fidx=0;
		for(ZSPair<?,?> entry : logic){
			String defin = (String)entry.getFirst();
			String[] items = defin.split("\\.");
			if(items[1].equals("property")&&items[2].equals(_name)){
				prodef=defin;

			fidx=idx;
				
			}
			idx++;
			
		}
		if(fidx>0&&prodef!=null)
		logic.set(fidx, new ZSPair<String,ZSSingle<?>>(prodef,new ZSSingle<String>(newstring) ));
		
	}
	
	// INTEGER
public void AddIntegerProperty(String def, int val){
		
		this.logic.add(new ZSPair<String,ZSSingle<?>>(def,new ZSSingle<Integer>(val)));
	
	}
	
	public Integer getIntegerProperty(String name){
		
		for(ZSPair<?,?> entry : logic){
			String defin = (String)entry.getFirst();
			String[] items = defin.split("\\.");
			if(items[1].equals("property")&&items[2].equals(name)){
				ZSSingle<?> v = (ZSSingle<?>)entry.getSecond();
				return (Integer)v.getFirst();
			}
			
		}
		return null;
	}
	
	public void setIntegerProperty(String _name,Integer newint){
		
		String prodef=null;
		int idx=0,fidx=0;
		for(ZSPair<?,?> entry : logic){
			String defin = (String)entry.getFirst();
			String[] items = defin.split("\\.");
			if(items[1].equals("property")&&items[2].equals(_name)){
				prodef=defin;

			fidx=idx;
				
			}
			idx++;
			
		}
		if(fidx>0&&prodef!=null)
		logic.set(fidx, new ZSPair<String,ZSSingle<?>>(prodef,new ZSSingle<Integer>(newint) ));
		
	}
	
	public void AddAction(String n,ZSLogicUnit lu){
		
		this.logic.add(new ZSPair<String,ZSLogicUnit>(n,lu));
	}
	
	public ZSSingle<?> _(String n, ZSLUParameters params){
		
		for(ZSPair<?,?> entry : logic){
			String defin = (String)entry.getFirst();
			String[] items = defin.split("\\.");
			if(items[1].equals("action")&&items[2].equals(n)){
				
				ZSLogicUnit cb = (ZSLogicUnit)entry.getSecond();
				cb.is(this, params);
				if (params!=null)
				return params.getResult();
			}
			
		}
		return null;
		
	}
	
	public String _S(String n, ZSLUParameters params){
		
		for(ZSPair<?,?> entry : logic){
			String defin = (String)entry.getFirst();
			String[] items = defin.split("\\.");
			if(items[1].equals("action")&&items[2].equals(n)){
				
				ZSLogicUnit cb = (ZSLogicUnit)entry.getSecond();
				cb.is(this, params);
				if (params!=null)
				return params.getStringResult();
			}
			
		}
		return null;
		
	}
	
	public int _I(String n, ZSLUParameters params){
		
		for(ZSPair<?,?> entry : logic){
			String defin = (String)entry.getFirst();
			String[] items = defin.split("\\.");
			if(items[1].equals("action")&&items[2].equals(n)){
				
				ZSLogicUnit cb = (ZSLogicUnit)entry.getSecond();
				cb.is(this, params);
				if (params!=null)
				return params.getIntResult();
			}
			
		}
		return 0;
		
	}
	
	public  void on(String c, Callback code)	{
		String[] items = c.split("\\.");
	
	if (items.length==3&&!items[0].contains("(")&&!items[1].contains("(")&&!items[2].contains("(")){
		
		names.add(c);
		cbs.add(code);
	
	}
	}
	
	public List<String> getNamesList(){
		
		return this.names;
	}
	
	public List<Callback> getListenersList(){
		
		return this.cbs;
	}
	protected void finalize(){
		//Log.v("gtrans"," destroy component ");
		
	}
}
